/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package app.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import org.rails.extjs.AbsBase;
import org.rails.extjs.ExtJS;
import org.rails.extjs.chart.BarChart;
import org.rails.extjs.chart.CategoryAxis;
import org.rails.extjs.chart.ColumnChart;
import org.rails.extjs.chart.LineChart;
import org.rails.extjs.chart.NumericAxis;
import org.rails.extjs.chart.PieChart;
import org.rails.extjs.chart.StackedBarChart;
import org.rails.extjs.chart.StackedColumnChart;
import org.rails.extjs.chart.StackedSeries;
import org.rails.extjs.data.Field;
import org.rails.extjs.data.JsonStore;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.json.Json;
import org.rails.extjs.panel.Panel;
import org.rails.extjs.panel.Viewport;
import org.rails.core.web.Action;
import org.rails.core.web.ControllerBase;

public final class ChartController extends ControllerBase {

	public ChartController(Action action) throws Exception {
		super(action);
		//AbsBase.setDebug(true);
		logger.info("=============");
		
	}
	
	public void barChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("BarChart demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getBarData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("month", "month"));
		store.addField(new Field("total", "total"));
		
		final BarChart chart = new BarChart();
		chart.setStore(store);		
		chart.setXField("total");
		chart.setYField("month");		
		final CategoryAxis ya = new CategoryAxis("Month");
		chart.setYAxis(ya);		
		final NumericAxis xa = new NumericAxis("Total");		
		xa.setLabelRenderer(new JavaScript("Ext.util.Format.numberRenderer('0,0')"));
		chart.setXAxis(xa);
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}
	
	public void columnChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("ColumnChart demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getColumnData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("month", "month"));
		store.addField(new Field("total", "total"));
		
		final ColumnChart chart = new ColumnChart();
		chart.setStore(store);		
		chart.setXField("month");
		chart.setYField("total");		
		final CategoryAxis xa = new CategoryAxis("Month");
		chart.setXAxis(xa);		
		final NumericAxis ya = new NumericAxis("Total");		
		ya.setLabelRenderer(new JavaScript("Ext.util.Format.numberRenderer('0,0')"));
		chart.setYAxis(ya);
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}
	
	public void pieChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("PieChar demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getPieData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("season", "season"));
		store.addField(new Field("total", "total"));
		
		final PieChart chart = new PieChart();
		chart.setStore(store);		
		chart.setDataField("total");
		chart.setCategoryField("season");
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}
	
	public void lineChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("LineChart demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getLineData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("season", "season"));
		store.addField(new Field("total", "total"));
		
		final LineChart chart = new LineChart();
		chart.setStore(store);
		chart.setXField("season");
		chart.setYField("total");
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}
	
	public void stackedBarChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("StackedBarChart demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getStackedData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("year", "year"));
		store.addField(new Field("comedy", "comedy"));
		store.addField(new Field("action", "action"));
		store.addField(new Field("drama", "drama"));
		store.addField(new Field("thriller", "thriller"));
		final StackedBarChart chart = new StackedBarChart();
		chart.setStore(store);
		chart.setYField("year");
		
		NumericAxis xa = new NumericAxis("Total");
		xa.setStackingEnabled(true);
		xa.setLabelRenderer(new JavaScript("Ext.util.Format.usMoney"));
		chart.setXAxis(xa);
		
		StackedSeries bser = new StackedSeries();
		bser.setXField("comedy");
		bser.setDisplayName("Comedy");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setXField("action");
		bser.setDisplayName("Action");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setXField("drama");
		bser.setDisplayName("Drama");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setXField("thriller");
		bser.setDisplayName("Thriller");
		chart.addSeries(bser);
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}
	
	public void stackedColumnChartAction() throws IOException, ServletException{		
		final Viewport view = new Viewport();
		final Panel colPan = new Panel();
		colPan.setTitle("StackedColumnChart demo");
		colPan.setWidth(600);
		colPan.setHeight(400);	
		
		final JsonStore store = new JsonStore();
		final Json data = new Json();		
		data.put("data",DataList.getStackedData());
		store.setData(data);
		store.setRoot("data");
		store.addField(new Field("year", "year"));
		store.addField(new Field("comedy", "comedy"));
		store.addField(new Field("action", "action"));
		store.addField(new Field("drama", "drama"));
		store.addField(new Field("thriller", "thriller"));
		final StackedColumnChart chart = new StackedColumnChart();
		chart.setStore(store);
		chart.setXField("year");
		
		NumericAxis ya = new NumericAxis("Total");
		ya.setLabelRenderer(new JavaScript("Ext.util.Format.usMoney"));
		chart.setYAxis(ya);
		
		StackedSeries bser = new StackedSeries();
		bser.setYField("comedy");
		bser.setDisplayName("Comedy");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setYField("action");
		bser.setDisplayName("Action");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setYField("drama");
		bser.setDisplayName("Drama");
		chart.addSeries(bser);
		bser = new StackedSeries();
		bser.setYField("thriller");
		bser.setDisplayName("Thriller");
		chart.addSeries(bser);
		
		colPan.addItem(chart);		
		view.addItem(colPan);
		ExtJS.bind(request,view);
		action.render();
	}

}
